package sample1;
import lovedna.game.Actor;
import lovedna.game.Mesh;
import lovedna.game.Shader;
import lovedna.game.ShaderCode;
import lovedna.game.components.MeshRenderer;
import lovedna.game.geometry.GeomPlane;
import lovedna.resource.Assets;


/**
 * ... 
 * @author liwei
 */
class WaterEff extends Actor
{
	/*======constants============================*/
	
	/*======static ==============================*/
	
	/*======Public===============================*/
	
	/*======Private==============================*/
	
	public function new() 
	{
		super();
		
		Client.instance.camera.actor.addComponent(lovedna.game.components.CameraControl);
		var actor:Actor = new Actor();
		var mr:MeshRenderer = actor.addComponent(MeshRenderer);
		
		
		var frag:String = "
		uniform sampler2D tex:TEX;
 
uniform vec4 _time:TIME;
uniform vec4 resolution:TARGET_PARAMS;
//const float PI = 3.1415926535897932;
 
//speed
const float speed = 0.2;
const float speed_x = 0.3;
const float speed_y = 0.3;
 
// geometry
const float intensity = 3.;
const int steps = 8;
const float frequency = 4.0;
const int angle = 7; // better when a prime
 
// reflection and emboss
const float delta = 20.;
const float intence = 400.;
const float emboss = 0.3;
 
//---------- crystals effect
 
float col(vec2 coord)
{
    float delta_theta = 2.0 * PI / float(angle);
    float col = 0.0;
    float theta = 0.0;
	float time = _time.y;
    for (int i = 0; i < steps; i++)
    {
        vec2 adjc = coord;
        theta = delta_theta*float(i);
        adjc.x += cos(theta)*time*speed + time * speed_x;
        adjc.y -= sin(theta)*time*speed - time * speed_y;
        col = col + cos( (adjc.x*cos(theta) - adjc.y*sin(theta))*frequency)*intensity;
    }
 
    return cos(col);
}
 
void main()
{
    vec2 p = (gl_FragCoord.xy) / resolution.xy, c1 = p, c2 = p;
    float cc1 = col(c1);
 
    c2.x += resolution.x/delta;
    float dx = emboss*(cc1-col(c2))/delta;
 
    c2.x = p.x;
    c2.y += resolution.y/delta;
    float dy = emboss*(cc1-col(c2))/delta;
 
    c1.x += dx;
    c1.y = -(c1.y+dy);
 
    float alpha = 1.+dot(dx,dy)*intence;
    gl_FragColor = texture2D(tex,c1)*(alpha);
}
		";
		
		mr.material.shader = new Shader(new ShaderCode("", null, frag));
		//mr.material.state.cullMode = GLState.CULLMODE_FRONT;
		mr.material.setTexture(Assets.texture.get("image/grid.jpg").texture);
		mr.mesh = new Mesh();
		var s = new GeomPlane();
		//s.width = s.height = s.depth = 100;
		s.width = s.height = 100;
		mr.mesh.fromBuffer(s.build());
		//actor.transform.rotation.setEulerAngles(0, 45, 40);
		actor.setParent(this);
	}
	/*======Public Static methods================*/
	
	/*======Public methods=======================*/
	
	/*======public override methods==============*/
	
	/*======private override methods=============*/
	
	/*======Private methods======================*/
	
	/*======get set methods======================*/
	
}